package com.businessportal.web.dataaccess.dao;

import com.businessportal.web.dataaccess.entities.Login;
import com.businessportal.web.dataaccess.utils.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Vadim on 6/27/14.
 */
public class UserDao implements IUserDao{

    @Override
    public Login getById(Long aLong) {

        Session session = null;
        Login retValue = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            retValue = (Login)session.get(Login.class, aLong);

            session.getTransaction().commit();
        } catch (Exception e){

            retValue = null;
            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }

        return retValue;
    }

    @Override
    public List<Login> getAll() {

        Session session = null;
        List<Login> retValue = null;

        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            Query userQuery = session.createQuery("From Login where 1=1");

            retValue = userQuery.list();

            session.getTransaction().commit();

        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }

        return retValue;
    }

    @Override
    public void save(Login login) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.save(login);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public void update(Login login) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.saveOrUpdate(login);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public void delete(Login login) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.delete(login);

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public void deleteById(Long aLong) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            session.delete(new Login(aLong));

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                session.close();
            }
        }
    }

    @Override
    public Login CheckLoginPass(String login, String pass) {
        Session session = null;
        Login retValue = null;
        try{
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            String sql = String.format("From Login where login='%s' and password='%s'", login, pass);

            Query userQuery = session.createQuery(sql);

            List<Login> retList = userQuery.list();

            if(null != retList && retList.size() == 1){
                retValue = retList.get(0);
            }

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                //session.close();
            }
        }

        return retValue;
    }

    @Override
    public boolean IsUserNameAcceptable(String login) {
        Session session = null;
        boolean retValue = false;
        try{
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            String sql = String.format("From Login where login='%s'", login);

            Query userQuery = session.createQuery(sql);

            List<Login> retList = userQuery.list();

            if(null != retList && retList.isEmpty()){
                retValue = true;
            }

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                //session.close();
            }
        }

        return retValue;
    }

    @Override
    public Login GetLoginByEmail(String login) {
        Session session = null;
        Login retValue = null;
        try{
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            String sql = String.format("From Login where login='%s'", login);

            Query userQuery = session.createQuery(sql);

            List<Login> retList = userQuery.list();

            if(null != retList && !retList.isEmpty()){
                retValue = retList.get(0);
            }

            session.getTransaction().commit();
        } catch (Exception e){

            session.getTransaction().rollback();

            e.printStackTrace();
        } finally {
            if(null != session){
                //session.close();
            }
        }

        return retValue;
    }
}
